#!/bin/bash

# 修复FFmpeg安装脚本
# 解决阿里云镜像站403 Forbidden错误

# 检查是否以root权限运行
if [ "$EUID" -ne 0 ]
  then echo "请以root权限运行此脚本 (sudo)"
  exit
fi

# 备份当前的sources.list
echo "正在备份当前的软件源配置..."
cp /etc/apt/sources.list /etc/apt/sources.list.backup.$(date +%Y%m%d)

# 更新软件源为官方源或其他可用镜像源
echo "正在更新软件源配置..."
cat > /etc/apt/sources.list << EOF
deb http://archive.ubuntu.com/ubuntu/ focal main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu/ focal-security main restricted universe multiverse
EOF

# 更新软件包列表
echo "正在更新软件包列表..."
apt-get update

# 使用--fix-missing参数尝试安装FFmpeg
echo "正在尝试安装FFmpeg..."
apt-get install -y --fix-missing ffmpeg libavcodec-dev libavformat-dev libavutil-dev

# 安装完成后检查FFmpeg是否安装成功
if command -v ffmpeg &> /dev/null
then
    echo "FFmpeg安装成功!"
    ffmpeg -version
else
    echo "FFmpeg安装失败，请尝试以下备用方法："
    echo "1. 手动从FFmpeg官网下载源码编译安装"
    echo "2. 尝试使用其他软件源"
    echo "3. 检查网络连接和防火墙设置"
fi

# 提供额外的源码编译指南
echo "\n如果需要从源码编译FFmpeg，可以参考以下步骤："
echo "1. 安装依赖：apt-get install -y build-essential yasm cmake libtool libc6 libc6-dev unzip wget"
echo "2. 下载源码：wget https://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2"
echo "3. 解压源码：tar -xvjf ffmpeg-snapshot.tar.bz2"
echo "4. 编译安装：cd ffmpeg && ./configure --enable-shared --prefix=/usr/local && make -j$(nproc) && make install"
echo "5. 配置库路径：echo /usr/local/lib >> /etc/ld.so.conf.d/ffmpeg.conf && ldconfig"